package com.yuju.controller.hotel;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.enums.StatusEnum;
import com.yuju.hotel.enums.RoomReturnCodeEnum;
import com.yuju.hotel.model.RoomModel;
import com.yuju.hotel.service.IRoomService;
import com.yuju.log.service.IRoomLogService;
import com.yuju.util.Utils;
import com.yuju.vo.hotel.RoomVO;
/**
 * 
 * 类名：RoomController  <br />
 *
 * 功能：房间信息相关接口
 *
 * @author : zhousc <br />
 * @Date : 2017年11月27日 下午4:38:24  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("room")
public class RoomController extends BaseController<RoomVO, RoomModel> {

	@Autowired
	private IRoomService roomService;
	/**
	 * 
	 * 功能：<br/>房间列表
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 下午4:06:32 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		RoomVO  roomVO = obj.toJavaObject(RoomVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(roomVO, "pager", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		PageModel pageModel = roomService.pageList(convert(roomVO), roomVO.getPage(), roomVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomReturnCodeEnum.ROOM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：<br/>新增房间
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 下午4:07:03 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		RoomVO  roomVO = obj.toJavaObject(RoomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomVO, "save", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		int rows = roomService.save(convert(roomVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomReturnCodeEnum.ROOM_SAVE_SUCCESS.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomReturnCodeEnum.ROOM_SAVE_FAILURE.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：<br/>更新房间
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 下午4:07:16 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		RoomVO  roomVO = obj.toJavaObject(RoomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomVO, "update", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		int rows = roomService.updateSelective(convert(roomVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomReturnCodeEnum.ROOM_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomReturnCodeEnum.ROOM_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：<br/>房间删除
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 下午4:07:35 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		RoomVO  roomVO = obj.toJavaObject(RoomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomVO, "del", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		int rows = roomService.del(roomVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(RoomReturnCodeEnum.ROOM_DEL_SUCCESS.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomReturnCodeEnum.ROOM_DEL_FAILURE.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>根据房间ID获取房间信息
	 *
	 * @author zhousc
	 * @Date 2017年11月27日 下午4:07:49 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		RoomVO  roomVO = obj.toJavaObject(RoomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomVO, "get", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		RoomModel model = roomService.findById(roomVO.getId());
		if (Utils.isNotNull(model)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(model);
			result.setReturnCode(RoomReturnCodeEnum.ROOM_QUERY_SUCCESS.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：重名验证<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月14日 上午10:52:17 <br/>
	 */
	@RequestMapping("selectbyname")
	@ResponseBody
	public Result selectbyname(@RequestBody JSONObject obj) {
		RoomVO  roomVO = obj.toJavaObject(RoomVO.class);
		Map<String, String> fail = FieldValidator.validateAll(roomVO, "selectbyname", true);
		if (!fail.isEmpty()) {
				return validateError(fail);
			}
		Result result = new Result();
		boolean selectByName = roomService.selectByName(convert(roomVO));
		if (selectByName) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：房间锁房操作<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月14日 下午3:42:34 <br/>
	 */
	@RequestMapping("lockRoom")
	@ResponseBody
	public Result lockRoom(@RequestBody JSONObject obj) {
		RoomVO  roomVO = obj.toJavaObject(RoomVO.class);
		Result result = new Result();
		
		RoomModel roomModel = new RoomModel();
		roomModel.setId(roomVO.getId());
		//判断当前状态
		if(roomVO.getIsLock().equals(StatusEnum.TRUE.getValue())){
			roomModel.setIsLock(StatusEnum.FALSE.getValue());
		} else {
			roomModel.setIsLock(StatusEnum.TRUE.getValue());
		}
		int i = roomService.updateSelective(roomModel);
		if (Utils.isNotNull(i)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(i);
			result.setReturnCode(RoomReturnCodeEnum.ROOM_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomReturnCodeEnum.ROOM_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：房间打扫操作<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月14日 下午3:47:10 <br/>
	 */
	@RequestMapping("dirtyRoom")
	@ResponseBody
	public Result dirtyRoom(@RequestBody JSONObject obj) {
		RoomVO  roomVO = obj.toJavaObject(RoomVO.class);
		Result result = new Result();
		RoomModel roomModel = new RoomModel();
		roomModel.setId(roomVO.getId());
		//判断当前状态
		if(roomVO.getIsDirty().equals(StatusEnum.TRUE.getValue())){
			roomModel.setIsDirty(StatusEnum.FALSE.getValue());
		} else {
			roomModel.setIsDirty(StatusEnum.TRUE.getValue());
		}
		int i = roomService.updateSelective(roomModel);
		if (Utils.isNotNull(i)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(i);
			result.setReturnCode(RoomReturnCodeEnum.ROOM_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(RoomReturnCodeEnum.ROOM_UPDATE_FAILURE.getCode());
			result.setReturnMsg(RoomReturnCodeEnum.ROOM_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
}
